//
//  VenueVipTableViewCell.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/26.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "VenueVipTableViewCell.h"

@implementation VenueVipTableViewCell

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        /**
         头像
         */
        UIImageView *headImageView = [[UIImageView alloc] init];
        headImageView.backgroundColor = CSRedColor;
        headImageView.layer.cornerRadius = 25;
        headImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:headImageView];
        self.headImageView = headImageView;
        
        /**
         昵称
         */
        UILabel *nickNameLabel = [[UILabel alloc] init];
        nickNameLabel.font = MidBigFont;
        nickNameLabel.textColor = CSColorFromRGB(0x000000);
        [self.contentView addSubview:nickNameLabel];
        self.nickNameLabel = nickNameLabel;
        
        
        /**
         性别
         
         */
        UIButton *sexBtn = [[UIButton alloc] init];
        [sexBtn setImage:@"icon_female"];
        [sexBtn setSelectedImage:@"icon_male"];
        [self.contentView addSubview:sexBtn];
        self.sexBtn = sexBtn;
        
        
        
        /**
         电话
         */
        UILabel *phoneLabel = [[UILabel alloc] init];
        phoneLabel.font = [UIFont systemFontOfSize:13.0];;
        phoneLabel.textColor = CSColorFromRGB(0x9e9e9e);
        [self.contentView addSubview:phoneLabel];
        self.phoneLabel = phoneLabel;
        
        
        /**
         年龄
         */
        UIButton *ageBtn = [[UIButton alloc] init];
        ageBtn.layer.borderWidth = 0.5;
        ageBtn.layer.borderColor = BackGroundColor.CGColor;
        ageBtn.titleLabel.font = SmallSmallFont;
        ageBtn.contentEdgeInsets = UIEdgeInsetsMake(3, 5, 3, 5);
        [ageBtn setTitle:@"22岁"];
        [ageBtn setTitleColor:CSColorFromRGB(0x9e9e9e)];
        [self.contentView addSubview:ageBtn];
        self.ageBtn = ageBtn;
        
        /**
         右侧
         */
        UIButton *rightImageView = [[UIButton alloc] init];
        [rightImageView setImage:@"xiayibu"];
        [self.contentView addSubview:rightImageView];
        self.rightImageView = rightImageView;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    /**
     头像
     */
    [self.headImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(self.contentView).offset(10);
        
        make.top.mas_equalTo(self.contentView).offset(15);
        
        make.width.height.mas_equalTo(50);
        
        make.bottom.mas_equalTo(self.contentView).offset(-15);
    }];
    
    /**
     昵称
     */
    [self.nickNameLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.headImageView);
        
        make.left.mas_equalTo(self.headImageView.mas_right).offset(15);
    }];
    
    /**
     性别
     
     */
    [self.sexBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.nickNameLabel);
        
        make.left.mas_equalTo(self.nickNameLabel.mas_right).offset(5);
    }];
    
    /**
     年龄
     */
    [self.ageBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.sexBtn);
        
        make.left.mas_equalTo(self.sexBtn.mas_right).offset(5);
    }];
    
    /**
     电话
     */
    [self.phoneLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.nickNameLabel.mas_bottom).offset(10);
        
        make.left.mas_equalTo(self.nickNameLabel);
    }];
    
    
    /**
     右侧
     */
    [self.rightImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.contentView);
        
        make.right.mas_equalTo(self.contentView).offset(-5);
    }];
}


-(void)setVenueVipModel:(VenueVipModel *)venueVipModel{
    
    //0 是男
    if ([venueVipModel.Sex integerValue] == 0) {
        
        self.sexBtn.selected = YES;
    }
    else{
        
        self.sexBtn.selected = NO;
    }
    
    self.phoneLabel.text = [NSString stringWithFormat:@"联系电话：%@",venueVipModel.CellPhone];
    
    if (venueVipModel.Birthday) {
     
        [self.ageBtn setTitle:[NSString stringWithFormat:@"%ld岁",[self getAgeWithBirth:venueVipModel.Birthday]]];
    }
    
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",venueVipModel.Pic,mid_image];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];            
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:PlaceHold];
}

-(NSInteger)getAgeWithBirth:(NSString *)birth{
    
    NSDateFormatter *dateFomatter = [[NSDateFormatter alloc] init];
    
    dateFomatter.dateFormat = @"yyyy-MM-dd";
    
    // 截止时间data格式
    NSDate *expireDate = [dateFomatter dateFromString:birth];
    
    // 当前时间data格式
    NSDate *nowDate = [NSDate date];
    // 当前日历
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 需要对比的时间数据
    NSCalendarUnit unit = NSCalendarUnitYear | NSCalendarUnitMonth
    | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 对比时间差
    NSDateComponents *dateCom = [calendar components:unit fromDate:expireDate toDate:nowDate options:0];
    
    return dateCom.year;
    
    
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context =UIGraphicsGetCurrentContext();
    
    CGContextBeginPath(context);
    
    CGContextSetLineWidth(context,1.0);//线宽度
    
    CGContextSetStrokeColorWithColor(context,LineBackGroundColor.CGColor);
    
    CGContextMoveToPoint(context,self.headImageView.left,rect.size.height);
    
    CGContextAddLineToPoint(context,SCREEN_WIDTH,rect.size.height);
    
    CGContextStrokePath(context);
}

@end
